# -*- coding: utf-8 -*-
import translator
import recipe
import checkURL

langDict = {'AFRIKAANS' : 'af',
            'ALBANIAN' : 'sq',
            'AMHARIC' : 'am',
            'ARABIC' : 'ar',
            'ARMENIAN' : 'hy',
            'AZERBAIJANI' : 'az',
            'BASQUE' : 'eu',
            'BELARUSIAN' : 'be',
            'BENGALI' : 'bn',
            'BIHARI' : 'bh',
            'BRETON' : 'br',
            'BULGARIAN' : 'bg',
            'BURMESE' : 'my',
            'CATALAN' : 'ca',
            'CHEROKEE' : 'chr',
            'CHINESE' : 'zh',
            'CHINESE_SIMPLIFIED' : 'zh-CN',
            'CHINESE_TRADITIONAL' : 'zh-TW',
            'CORSICAN' : 'co',
            'CROATIAN' : 'hr',
            'CZECH' : 'cs',
            'DANISH' : 'da',
            'DHIVEHI' : 'dv',
            'DUTCH': 'nl',  
            'ENGLISH' : 'en',
            'ESPERANTO' : 'eo',
            'ESTONIAN' : 'et',
            'FAROESE' : 'fo',
            'FILIPINO' : 'tl',
            'FINNISH' : 'fi',
            'FRENCH' : 'fr',
            'FRISIAN' : 'fy',
            'GALICIAN' : 'gl',
            'GEORGIAN' : 'ka',
            'GERMAN' : 'de',
            'GREEK' : 'el',
            'GUJARATI' : 'gu',
            'HAITIAN_CREOLE' : 'ht',
            'HEBREW' : 'iw',
            'HINDI' : 'hi',
            'HUNGARIAN' : 'hu',
            'ICELANDIC' : 'is',
            'INDONESIAN' : 'id',
            'INUKTITUT' : 'iu',
            'IRISH' : 'ga',
            'ITALIAN' : 'it',
            'JAPANESE' : 'ja',
            'JAVANESE' : 'jw',
            'KANNADA' : 'kn',
            'KAZAKH' : 'kk',
            'KHMER' : 'km',
            'KOREAN' : 'ko',
            'KURDISH': 'ku',
            'KYRGYZ': 'ky',
            'LAO' : 'lo',
            'LATIN' : 'la',
            'LATVIAN' : 'lv',
            'LITHUANIAN' : 'lt',
            'LUXEMBOURGISH' : 'lb',
            'MACEDONIAN' : 'mk',
            'MALAY' : 'ms',
            'MALAYALAM' : 'ml',
            'MALTESE' : 'mt',
            'MAORI' : 'mi',
            'MARATHI' : 'mr',
            'MONGOLIAN' : 'mn',
            'NEPALI' : 'ne',
            'NORWEGIAN' : 'no',
            'OCCITAN' : 'oc',
            'ORIYA' : 'or',
            'PASHTO' : 'ps',
            'PERSIAN' : 'fa',
            'POLISH' : 'pl',
            'PORTUGUESE' : 'pt',
            'PORTUGUESE_PORTUGAL' : 'pt-PT',
            'PUNJABI' : 'pa',
            'QUECHUA' : 'qu',
            'ROMANIAN' : 'ro',
            'RUSSIAN' : 'ru',
            'SANSKRIT' : 'sa',
            'SCOTS_GAELIC' : 'gd',
            'SERBIAN' : 'sr',
            'SINDHI' : 'sd',
            'SINHALESE' : 'si',
            'SLOVAK' : 'sk',
            'SLOVENIAN' : 'sl',
            'SPANISH' : 'es',
            'SUNDANESE' : 'su',
            'SWAHILI' : 'sw',
            'SWEDISH' : 'sv',
            'SYRIAC' : 'syr',
            'TAJIK' : 'tg',
            'TAMIL' : 'ta',
            'TATAR' : 'tt',
            'TELUGU' : 'te',
            'THAI' : 'th',
            'TIBETAN' : 'bo',
            'TONGA' : 'to',
            'TURKISH' : 'tr',
            'UKRAINIAN' : 'uk',
            'URDU' : 'ur',
            'UZBEK' : 'uz',
            'UIGHUR' : 'ug',
            'VIETNAMESE' : 'vi',
            'WELSH' : 'cy',
            'YIDDISH' : 'yi',
            'YORUBA' : 'yo'}    

for key, value in langDict.iteritems():
    print "For language:", key, " ---- Select:", value


def selectLanguage(question):
    checksValid = True
    while checksValid:
        language = raw_input(question)
        for key, value in langDict.iteritems():
            if language == value:
                print 'You seleced:', key 
                checksValid = False
                break
        if checksValid:
            print 'Not a valid language' 
    return language

langSource = selectLanguage("What is the language of the source to translate? ")
langTarget = selectLanguage("What is the language of the desired translation? ")

checksValid = True
while checksValid:
    urlLink = raw_input("What is the html link to be translated? ")
    if checkURL.httpExists(urlLink):
        checksValid = False 


saveFile = raw_input("File name to be save in tmp folder: ")

repeatedWords = {}
saveFile = "/tmp/" + saveFile
textFile = open(saveFile, 'w')
for line in translator.fixHTML(urlLink):
    for word in line.split(' '):
        if not repeatedWords.has_key(word):
            repeatedWords[word] = translator.translate(word, langSource, langTarget)
        print repeatedWords[word],
        textFile.write(repeatedWords[word].encode('utf-8'))
        textFile.write(' ')
    print
    textFile.write('\n')
textFile.close()

recipe.main(saveFile)
